package com.gitee.jmliu1983.javautils;

import java.util.Iterator;

import com.alibaba.fastjson2.JSONObject;




public class SnakeToCamelCaseConverter {  
	  
    public static JSONObject convertToCamelCase(JSONObject jsonObject) {  
        JSONObject result = new JSONObject();  
        Iterator<String> keys = jsonObject.keySet().iterator();  
  
        while (keys.hasNext()) {  
            String key = keys.next();  
            String camelCaseKey = convertToCamelCase(key);  
            result.put(camelCaseKey, jsonObject.get(key));  
  
            // 如果原始值是另一个JSONObject，则递归转换  
            if (jsonObject.get(key) instanceof JSONObject) {  
                result.put(camelCaseKey, convertToCamelCase((JSONObject) jsonObject.get(key)));  
            }  
        }  
  
        return result;  
    }  
  
    private static String convertToCamelCase(String snakeCase) {  
        StringBuilder camelCase = new StringBuilder();  
        boolean nextUpperCase = false;  
  
        for (char c : snakeCase.toCharArray()) {  
            if (c == '_') {  
                nextUpperCase = true;  
            } else if (nextUpperCase) {  
                camelCase.append(Character.toUpperCase(c));  
                nextUpperCase = false;  
            } else {  
                camelCase.append(c);  
            }  
        }  
  
        return camelCase.toString();  
    }  
  
}